package com.bop.dehong.category;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.category.adapter.CateListAdapter;
import com.bop.dehong.category.adapter.CategoryPagerAdapter;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.ToubiaoActivity;
import com.bop.dehong.decaifu.ToubiaoTyjActivity;
import com.bop.dehong.home.adapter.HomeListAdapter;
import com.bop.dehong.http.model.CategoryModel;
import com.bop.dehong.info.entity.CategoryEntity;
import com.bop.dehong.info.entity.RecommendProject;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

public class DeCaiFuListActivity extends BaseActivity implements OnPageChangeListener, OnHeaderRefreshListener, OnFooterRefreshListener
{
	private ViewPager mViewPager;
	private List<View> mViews;
	private ListView mListView1;
	private ListView mListView2;
	private ListView mListView3;
	private ListView mListView4;
	private ListView mListView5;
	private LinearLayout ll_tab_1,ll_tab_2,ll_tab_3,ll_tab_4,ll_tab_5;
	private PullToRefreshView mPullToRefreshView1, mPullToRefreshView2, mPullToRefreshView3, mPullToRefreshView4, mPullToRefreshView5;
	private HomeListAdapter mAdapter1, mAdapter2, mAdapter3, mAdapter4, mAdapter5; 
	private int mCurrentIndex;
	private int totalpage1, totalpage2, totalpage3, totalpage4, totalpage5;
	private int mPage1 = 1, mPage2 = 1, mPage3 = 1, mPage4 = 1, mPage5 = 1;
	private CategoryModel categoryModel1, categoryModel2, categoryModel3, categoryModel4, categoryModel5;
	

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.ll_tab_1:
			ll_tab_1.setSelected(true);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(false);
			mViewPager.setCurrentItem(0);
			break;
		case R.id.ll_tab_2:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(true);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(false);
			mViewPager.setCurrentItem(1);
			break;
		case R.id.ll_tab_3:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(true);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(false);
			mViewPager.setCurrentItem(2);
			break;
		case R.id.ll_tab_4:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(true);
			ll_tab_5.setSelected(false);
			mViewPager.setCurrentItem(3);
			break;
		case R.id.ll_tab_5:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(true);
			mViewPager.setCurrentItem(4);
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		((HomeListAdapter)parent.getAdapter()).setSelectPosition(position);
		((HomeListAdapter)parent.getAdapter()).notifyDataSetChanged();
		Bundle bundle = new Bundle();
		RecommendProject entity = null;
		switch (mCurrentIndex) {
		case 0:
			entity = mAdapter1.get(position);
			break;
		case 1:
			entity = mAdapter2.get(position);
			break;
		case 2:
			entity = mAdapter3.get(position);
			break;
		case 3:
			entity = mAdapter4.get(position);
			break;
		case 4:
			entity = mAdapter5.get(position);
			break;
		default:
			break;
		}
//		Intent intent = null;
		bundle.putString(Config.CAT_ID, entity.getId());	
		bundle.putString(Config.CAT_NAME, entity.getTitle());
		
		int isemoney = Integer.valueOf(entity.getIsemoney());
		if (isemoney == 1) {
			openActivity(ToubiaoTyjActivity.class, bundle);
		}else {
			openActivity(ToubiaoActivity.class, bundle);
		}	
	}

	@Override
	protected void initTop()
	{
		View mView = getView(R.id.category_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.category);
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI()
	{
		 mViews = new ArrayList<View>();
			View view1 = View.inflate(this, R.layout.category_list, null);
			View view2 = View.inflate(this, R.layout.category_list, null);
			View view3 = View.inflate(this, R.layout.category_list, null);
			View view4 = View.inflate(this, R.layout.category_list, null);
			View view5 = View.inflate(this, R.layout.category_list, null);
			mViews.add(view1);
			mViews.add(view2);
			mViews.add(view3);
			mViews.add(view4);
			mViews.add(view5);
			mListView1 = getView(view1, R.id.category_list);
			mListView2 = getView(view2, R.id.category_list);
			mListView3 = getView(view3, R.id.category_list);
			mListView4 = getView(view4, R.id.category_list);
			mListView5 = getView(view5, R.id.category_list);
			mPullToRefreshView1 = getView(view1, R.id.coupon_list_pull_refresh_view);
			mPullToRefreshView2 = getView(view2, R.id.coupon_list_pull_refresh_view);  
			mPullToRefreshView3 = getView(view3, R.id.coupon_list_pull_refresh_view);  
			mPullToRefreshView4 = getView(view4, R.id.coupon_list_pull_refresh_view);  
			mPullToRefreshView5 = getView(view5, R.id.coupon_list_pull_refresh_view);  
			mPullToRefreshView1.setOnHeaderRefreshListener(this);
			mPullToRefreshView1.setOnFooterRefreshListener(this);
			mPullToRefreshView2.setOnHeaderRefreshListener(this);
			mPullToRefreshView2.setOnFooterRefreshListener(this);
			mPullToRefreshView3.setOnHeaderRefreshListener(this);
			mPullToRefreshView3.setOnFooterRefreshListener(this);
			mPullToRefreshView4.setOnHeaderRefreshListener(this);
			mPullToRefreshView4.setOnFooterRefreshListener(this);
			mPullToRefreshView5.setOnHeaderRefreshListener(this);
			mPullToRefreshView5.setOnFooterRefreshListener(this);
			mAdapter1 = new HomeListAdapter(this);
			mAdapter2 = new HomeListAdapter(this);
			mAdapter3 = new HomeListAdapter(this);
			mAdapter4 = new HomeListAdapter(this);
			mAdapter5 = new HomeListAdapter(this);
			mListView1.setAdapter(mAdapter1);
			mListView2.setAdapter(mAdapter2);
			mListView3.setAdapter(mAdapter3);
			mListView4.setAdapter(mAdapter4);
			mListView5.setAdapter(mAdapter5);
			mListView1.setOnItemClickListener(this);
			mListView2.setOnItemClickListener(this);
			mListView3.setOnItemClickListener(this);
			mListView4.setOnItemClickListener(this);
			mListView5.setOnItemClickListener(this);
		ll_tab_1 = getView(R.id.ll_tab_1);
		ll_tab_2 = getView(R.id.ll_tab_2);
		ll_tab_3 = getView(R.id.ll_tab_3);
		ll_tab_4 = getView(R.id.ll_tab_4);
		ll_tab_5 = getView(R.id.ll_tab_5);
		registerOnClickListener(ll_tab_1, ll_tab_2, ll_tab_3, ll_tab_4, ll_tab_5);
		mViewPager = getView(R.id.vPager);
		/***************** 左右滑动不销毁子View **********************/
		mViewPager.setOffscreenPageLimit(4);
		mViewPager.setAdapter(new CategoryPagerAdapter(mViews));
		mViewPager.setOnPageChangeListener(this);
	    ll_tab_1.setSelected(true);
	    mViewPager.setCurrentItem(mCurrentIndex);
	}

	@Override
	protected void initData()
	{
		switch (mCurrentIndex) {
		case 0:
			initData1();
			break;
		case 1:
			initData2();
			break;
		case 2:
			initData3();
			break;
		case 3:
			initData4();
			break;
		case 4:
			initData5();
			break;
		default:
			break;
		}
	}

	private void initData5() {
		if (mAdapter5 != null && mAdapter5.size() == 0){
			showProgressDialog();
			if (categoryModel5 == null) {
				categoryModel5 = new CategoryModel(this, callback5);
			}
			categoryModel5.doNet("5", mPage5 + "");	
		}
	}

	RequestCallBack<CategoryEntity> callback5 = new RequestCallBack<CategoryEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(CategoryEntity info) {
			if (info.list != null && info.list.size() > 0) {
				mAdapter5.appendToList(info.list);
			}else {
				ToastUtils.show(DeCaiFuListActivity.this, "暂无数据！");
			}
			totalpage5 = info.totalpages;
			super.onSuccess(info);
		};
		
	};
	
	private void initData4() {
		if (mAdapter4 != null && mAdapter4.size() == 0){
			showProgressDialog();
			if (categoryModel4 == null) {
				categoryModel4 = new CategoryModel(this, callback4);
			}
			categoryModel4.doNet("3", mPage4 + "");
		}
	}

	RequestCallBack<CategoryEntity> callback4 = new RequestCallBack<CategoryEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(CategoryEntity info) {
			if (info.list != null && info.list.size() > 0) {
				mAdapter4.appendToList(info.list);
			}else {
				ToastUtils.show(DeCaiFuListActivity.this, "暂无数据！");
			}
			totalpage4 = info.totalpages;
			super.onSuccess(info);
		};
		
	};
	
	private void initData3() {
		if (mAdapter3 != null && mAdapter3.size() == 0){
			showProgressDialog();
			if (categoryModel3 == null) {
				categoryModel3 = new CategoryModel(this, callback3);
			}
			categoryModel3.doNet("2", mPage3 + "");
		}
	}

	RequestCallBack<CategoryEntity> callback3 = new RequestCallBack<CategoryEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(CategoryEntity info) {
			if (info.list != null && info.list.size() > 0) {
				mAdapter3.appendToList(info.list);
			}else {
				ToastUtils.show(DeCaiFuListActivity.this, "暂无数据！");
			}
			totalpage3 = info.totalpages;
			super.onSuccess(info);
		};
		
	};
	
	private void initData2() {
		if (mAdapter2 != null && mAdapter2.size() == 0){
			showProgressDialog();
			if (categoryModel2 == null) {
				categoryModel2 = new CategoryModel(this, callback2);
			}
			categoryModel2.doNet("1", mPage2 + "");	
		}
	}

	RequestCallBack<CategoryEntity> callback2 = new RequestCallBack<CategoryEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(CategoryEntity info) {
			if (info.list != null && info.list.size() > 0) {
				mAdapter2.appendToList(info.list);
			}else {
				ToastUtils.show(DeCaiFuListActivity.this, "暂无数据！");
			}
			totalpage2 = info.totalpages;
			super.onSuccess(info);
		};
		
	};
	
	private void initData1() {
		if (mAdapter1 != null && mAdapter1.size() == 0)
		{
			showProgressDialog();
			if (categoryModel1 == null) {
				categoryModel1 = new CategoryModel(this, callback1);
			}
			categoryModel1.doNet("4", mPage1 + "");
		}	
	}
	
	RequestCallBack<CategoryEntity> callback1 = new RequestCallBack<CategoryEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(CategoryEntity info) {
			if (info.list != null && info.list.size() > 0) {
				mAdapter1.appendToList(info.list);
			}else {
				ToastUtils.show(DeCaiFuListActivity.this, "暂无数据！");
			}
			totalpage1 = info.totalpages;
			super.onSuccess(info);
		};
		
	};
	
	
	@Override
	protected int getContentViewId()
	{
		TAG = getClass().getName();
		return R.layout.activity_category;
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int position)
	{
		mCurrentIndex = position;
		switch (position) {
		case 0:
			ll_tab_1.setSelected(true);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(false);
			mPage1 = 1;
			mAdapter1.notifyDataSetChanged();
			initData1();
			break;
		case 1:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(true);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(false);
			mPage2 = 1;
			mAdapter2.notifyDataSetChanged();
			initData2();
			break;
		case 2:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(true);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(false);
			mPage3 = 1;
			mAdapter3.notifyDataSetChanged();
			initData3();
			break;
		case 3:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(true);
			ll_tab_5.setSelected(false);
			mPage4 = 1;
			mAdapter4.notifyDataSetChanged();
			initData4();
			break;
		case 4:
			ll_tab_1.setSelected(false);
			ll_tab_2.setSelected(false);
			ll_tab_3.setSelected(false);
			ll_tab_4.setSelected(false);
			ll_tab_5.setSelected(true);
			mPage5 = 1;
			mAdapter5.notifyDataSetChanged();
			initData5();
			break;
		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int state)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view)
	{
		switch (mCurrentIndex) {
		case 0:
					mPage1 = 1;
					mAdapter1.clear();
					showProgressDialog();
					if (categoryModel1 == null) {
						categoryModel1 = new CategoryModel(this, callback1);
					}
					categoryModel1.doNet("4", mPage1 + "");
					mPullToRefreshView1.onHeaderRefreshComplete();
			break;

		case 1:
					mPage2 = 1;
					mAdapter2.clear();
					showProgressDialog();
					if (categoryModel2 == null) {
						categoryModel2 = new CategoryModel(this, callback2);
					}
					categoryModel2.doNet("1", mPage2 + "");	
					mPullToRefreshView2.onHeaderRefreshComplete();
			break;
		case 2:
					mPage3 = 1;
					mAdapter3.clear();
					showProgressDialog();
					if (categoryModel3 == null) {
						categoryModel3 = new CategoryModel(this, callback3);
					}
					categoryModel3.doNet("2", mPage3 + "");
					mPullToRefreshView3.onHeaderRefreshComplete();
			break;
		case 3:
					mPage4 = 1;
					mAdapter4.clear();
					showProgressDialog();
					if (categoryModel4 == null) {
						categoryModel4 = new CategoryModel(this, callback4);
					}
					categoryModel4.doNet("3", mPage4 + "");
					mPullToRefreshView4.onHeaderRefreshComplete();
			break;
		case 4:
					mPage5 = 1;
					mAdapter5.clear();
					showProgressDialog();
					if (categoryModel5 == null) {
						categoryModel5 = new CategoryModel(this, callback5);
					}
					categoryModel5.doNet("5", mPage5 + "");	
					mPullToRefreshView5.onHeaderRefreshComplete();
			break;
		}
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view)
	{
		switch (mCurrentIndex) {
		case 0:
					if (mPage1 < totalpage1) {
						mPage1 ++;	
					}else {
						ToastUtils.show(this, "没有更多了!");
						mPullToRefreshView1.onFooterRefreshComplete();
						return;
					}
					showProgressDialog();
					if (categoryModel1 == null) {
						categoryModel1 = new CategoryModel(this, callback1);
					}
					categoryModel1.doNet("4", mPage1 + "");
					mPullToRefreshView1.onFooterRefreshComplete();
			break;
		case 1:
					if (mPage2 < totalpage2) {
						mPage2 ++;	
					}else {
						ToastUtils.show(this, "没有更多了!");
						mPullToRefreshView2.onFooterRefreshComplete();
						return;
					}
					showProgressDialog();
					if (categoryModel2 == null) {
						categoryModel2 = new CategoryModel(this, callback2);
					}
					categoryModel2.doNet("1", mPage2 + "");	
					mPullToRefreshView2.onFooterRefreshComplete();
			break;
		case 2:
					if (mPage3 < totalpage3) {
						mPage3 ++;	
					}else {
						ToastUtils.show(this, "没有更多了!");
						mPullToRefreshView3.onFooterRefreshComplete();
						return;
					}
					showProgressDialog();
					if (categoryModel3 == null) {
						categoryModel3 = new CategoryModel(this, callback3);
					}
					categoryModel3.doNet("2", mPage3 + "");
					mPullToRefreshView3.onFooterRefreshComplete();
			break;
		case 3:
					if (mPage4 < totalpage4) {
						mPage4 ++;	
					}else {
						ToastUtils.show(this, "没有更多了!");
						mPullToRefreshView4.onFooterRefreshComplete();
						return;
					}
					showProgressDialog();
					if (categoryModel4 == null) {
						categoryModel4 = new CategoryModel(this, callback4);
					}
					categoryModel4.doNet("3", mPage4 + "");
					mPullToRefreshView4.onFooterRefreshComplete();
			break;
		case 4:
					if (mPage5 < totalpage5) {
						mPage5 ++;	
					}else {
						ToastUtils.show(this, "没有更多了!");
						mPullToRefreshView5.onFooterRefreshComplete();
						return;
					}
					showProgressDialog();
					if (categoryModel5 == null) {
						categoryModel5 = new CategoryModel(this, callback5);
					}
					categoryModel5.doNet("5", mPage5 + "");	
					mPullToRefreshView5.onFooterRefreshComplete();
			break;
		}
	}

}
